package fei.typhoon.engine.android.sprite;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;
import fei.typhoon.engine.android.FeiScreen;
import fei.typhoon.engine.android.FeiSprite;
import fei.typhoon.engine.load.FeiBitmap;

public class FeiImage extends FeiSprite {

	/** 静态图片 */
	protected FeiBitmap bitmap = null;
	/** 变换计算用的矩阵 */
	protected Matrix matrix = null;

	public FeiImage(FeiScreen screen, FeiBitmap bitmap) {
		this.screen = screen;
		this.bitmap = bitmap;
		if (bitmap != null) {
			if (screen.isBitmapZoomReally()) {
				float skx = 1.0f, sky = 1.0f;
				if (screen.isKeepProportion()) {
					skx = screen.getScale();
					sky = screen.getScale();
				} else {
					skx = screen.getScaleX();
					sky = screen.getScaleY();
				}
				scaleOriginX = (float) bitmap.getWidth() / skx / 2.0f;
				scaleOriginY = (float) bitmap.getHeight() / sky / 2.0f;
				rotateOriginX = scaleOriginX;
				rotateOriginY = scaleOriginY;
			} else {
				scaleOriginX = (float) bitmap.getWidth() / 2.0f;
				scaleOriginY = (float) bitmap.getHeight() / 2.0f;
				rotateOriginX = scaleOriginX;
				rotateOriginY = scaleOriginY;
			}
		}
		paint = new Paint();
		paint.setAntiAlias(true);
		matrix = new Matrix();
	}

	public FeiImage(FeiScreen screen, FeiBitmap bitmap, float x, float y) {
		this(screen, bitmap);
		setPosition(x, y);
	}

	public Matrix getMatrix() {
		float skx = 1.0f, sky = 1.0f;
		float x = getX();
		float y = getY();
		float rox = getRotateOriginX(), roy = getRotateOriginY();
		float r = getRotate();
		float sox = getScaleOriginX(), soy = getScaleOriginY();
		float sx = getScaleX();
		float sy = getScaleY();

		matrix.reset();
		matrix.setRotate(r, rox, roy);
		if (screen.isKeepProportion()) {
			skx = screen.getScale();
			sky = screen.getScale();
		} else {
			skx = screen.getScaleX();
			sky = screen.getScaleY();
		}
		if (!screen.isBitmapZoomReally()) {
			// 模拟真实缩放
			matrix.postScale(skx, sky, 0, 0);
		}
		rox *= skx;
		roy *= sky;
		sox *= skx;
		soy *= sky;
		x *= skx;
		y *= sky;
		matrix.postScale(sx, sy, sox, soy);
		matrix.postTranslate(x, y);
		return matrix;
	}

	@Override
	public synchronized void draw(Canvas canvas) {
		if (bitmap != null) {
			try {
				Matrix matrix = getMatrix();
				bitmap.matrix(matrix);
				canvas.drawBitmap(bitmap.getBitmap(), matrix, paint);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public FeiBitmap getBitmap() {
		return bitmap;
	}

	public synchronized void setBitmap(FeiBitmap bitmap) {
		this.bitmap = bitmap;
	}

	public int getWidth() {
		if (bitmap == null) {
			return 0;
		}
		return bitmap.getWidth();
	}

	public int getHeight() {
		if (bitmap == null) {
			return 0;
		}
		return bitmap.getHeight();
	}

	public void setX(float x) {
		super.setX(x);
	}

	public void setY(float y) {
		super.setY(y);
	}

	public void setPosition(float x, float y) {
		super.setPosition(x, y);
	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {

	}

	@Override
	public synchronized void destroy() {
		if (bitmap != null) {
			bitmap.recycle();
			bitmap = null;
		}
	}

	@Override
	public final boolean touchDown(float x, float y) {
		touchedDown = false;
		if (bitmap != null && bitmap.getShape() != null) {
			String s = "";
			for (float f : bitmap.getShape().getVertexs()) {
				s += f + ", ";
			}
			Log.e("FeiImage", "---------touchDown bitmap vertexs : " + s
					+ "-------");
			if (bitmap.pointInRegion(x, y)) {
				touchedDown = onTouchDown(x, y);
			}
			if (touchedDown && canDrag) {
				dragX = x;
				dragY = y;
			}
		}

		return touchedDown;
	}

	@Override
	public final boolean touchMove(float x, float y) {
		if (touchedDown) {
			if (canDrag) {
				// 实现拖拽
				setPosition(this.x + (x - dragX), this.y + (y - dragY));
				dragX = x;
				dragY = y;
			}
			return onTouchMove(x, y);
		}
		return false;
	}

	@Override
	public final boolean touchUp(float x, float y) {
		if (touchedDown) {
			return onTouchUp(x, y);
		}
		return false;
	}

	@Override
	public boolean onTouchDown(float x, float y) {
		return false;
	}

	@Override
	public boolean onTouchMove(float x, float y) {
		return false;
	}

	@Override
	public boolean onTouchUp(float x, float y) {
		return false;
	}

}
